<template>
    <div>
        <el-form  label-width="80px" class="login-box">

             <el-button style="float: right" type="primary"
                       @click="addprescribe">提交药方</el-button>
            <el-table
                    :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
                    stripe
                    border
                    style="width: 100%">
                <el-table-column
                        prop="id"
                        label="序号"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="名称"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="类别"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="sell"
                        label="价格"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        prop="number"
                        label="剩余数量"
                        align="center"
                >
                </el-table-column>
                <el-table-column
                        label="数量"
                        align="center"
                >
                    <template #default="scope">
                        <el-button type="text" size="small" icon="el-icon-lx-roundadd"
                                   @click="addmedecines(scope.$index, scope.row)"></el-button>
                        {{scope.row.deleted}}
                        <el-button type="text" size="small" icon="el-icon-lx-move"
                                   @click="movemedecines(scope.$index, scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <center>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 20, 40]"
                    :page-size="pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="tableData.length"
            >
            </el-pagination>
        </center>
        </el-form>

    </div>
</template>

<script>
    import axios from "axios";
    import {ElMessage} from "element-plus";

    export default {
        name: "Prescribe",
        data(){
            return{
                name:'',
                resribe:{
                    deleted: 0,
                    doctorId: '',
                    id: '',
                    mid: '',
                    number: 0,
                    pid: ''
                },
                thisdate:'',
                resribes:{
                    deleted: 0,
                    doctorId: '',
                    id: '',
                    mid: '',
                    number: 0,
                    pid: '',
                    date:'',
                },
                resribes:[],
                tableData: {
                    id: '',
                    deleted:0,
                    name: '',
                    price:'',
                    sell:'',
                    sort:'',
                    specification:'',
                    vender:'',
                    batch:'',
                    dosage:'',
                    useWay:'',
                    beginDate   :'',
                    endDate:'',
                    number:0,
                },
                tableData:[],
                currentPage:1, //初始页
                pagesize:10,    //    每页的数据
                dialogFormVisible: false,
                i:0,
                did:'',
                pid:'',
                rid:'',
            }
        },
        activated(){
            this.currentTime();
            this.create();
            this.resribes=[];
        },
        methods: {
            create() {
                this.did=localStorage.getItem("id");
                this.pid=localStorage.getItem('pid')
                this.rid=this.$route.params.rid;
                axios.get("http://119.29.60.153:8001/doctorservice/medecine-info/getMedecinList").then(resp=> {
                    this.tableData= resp.data.data.medecines;
                    console.log(this.tableData);
                })
            },
            handleChange(value) {
                console.log(value);
            },
            addmedecines(index,row){
                row.deleted++;
                let i=0;
                for(i=0;i<this.resribes.length;i++){
                    if(row.id==this.resribes[i].mid){
                        this.resribes[i].number++;
                        return;
                    }
                }
                this.resribe.doctorId=this.did;
                this.resribe.mid=row.id;
                this.resribe.number=1;
                this.resribe.pid=this.pid;
                this.resribes.push({deleted: 0,
                    doctorId: this.resribe.doctorId,
                    id: '',
                    mid: this.resribe.mid,
                    number: this.resribe.number,
                    pid: this.resribe.pid,
                    date:this.thisdate
                });
                console.log(this.resribes);
            },
            movemedecines(index,row){
                if (row.deleted==0){
                    return;
                }
                row.deleted--;
                let i=0;
                for(i=0;i<this.resribes.length;i++){
                    if(row.id==this.resribes[i].mid){
                        if(this.resribes[i].number==1){
                            this.resribes.splice(i,1);
                            return;
                        }
                        this.resribes[i].number--;
                        return;
                    }
                }

            },
            addprescribe(){
                axios.post("http://119.29.60.153:8001/doctorservice/prescription/rescribeMedicine",this.resribes).then(resp=> {

                    if(resp.data.success===true){
                        ElMessage.success("开药成功");
                        this.$router.push({name:'PatientInf',params:{pid:this.pid,rid:this.rid}});

                    }else {
                        ElMessage.error("开药失败!");
                    }
                })
            },
            handleSizeChange: function (size) {
                this.pagesize = size;
                console.log(this.pagesize)  //每页下拉显示数据
            },
            handleCurrentChange: function(currentPage){
                this.currentPage = currentPage;
                console.log(this.currentPage)  //点击第几页
            },
            currentTime() {
                setInterval(this.formatDate, 500);
            },
            formatDate() {
                let date = new Date();
                let year = date.getFullYear(); // 年
                let month = date.getMonth() + 1; // 月
                let day = date.getDate(); // 日
                this.thisdate=`${year}-${month}-${day}`;
            }
        }
    }
</script>

<style scoped>
    .login-box{


    }
</style>